package gui.menu;

import gui.CanvasManager;

import java.io.IOException;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import application.MainController;

/**
 * Class for the Settings Menu.
 * @author Sandra Mayer
 *
 */
public class SettingsMenu extends Canvas implements CommandListener {
	
	private String updateRange = "Step update: ";
	private String updateTimeInterval = "Time Interval: ";
	private String updateNrOfFields = "Update radius: ";
	private Image backgroundImg;
	private Image cursorEImg;
	private Image cursorFImg;
	private static SettingsMenu instance;
	private Command okCmd;
	private Command chooseCmd;
	private byte[][] posOfCursor;
	private int pointerPos;
	private int nrOfItems = 9;
	
	/**
	 * Private Constructor.
	 *
	 */
	private SettingsMenu() {
		
		okCmd = new Command("Ok", Command.OK, 0);
		this.setCommandListener(this);
		this.addCommand(okCmd);
		
		chooseCmd = new Command("Choose", Command.OK, 0);
		this.addCommand(chooseCmd);
		
		try {
			backgroundImg = Image.createImage("/mainWhite.jpg");
			cursorEImg = Image.createImage("/rad11.JPG");
			cursorFImg = Image.createImage("/rad12.JPG");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Gets the instance of this singleton.
	 * @return the instance of the SettingsMenu
	 */
	public static SettingsMenu getInstance() {
		if (instance == null) {
			instance = new SettingsMenu();
			instance.initRadioButtons();
		}
		CanvasManager.setCurScreen(instance);
		return instance;
	}
	
	/**
	 * Paints the Menu.
	 */
	protected void paint(Graphics g) {
		g.setColor(0xffffff);
		g.fillRect(0, 0, getWidth(), getHeight());
		g.translate((getWidth() - backgroundImg.getWidth()) / 2,
				(getHeight() - backgroundImg.getHeight()) / 2);
		
		g.drawImage(backgroundImg, 0, 0, Graphics.TOP | Graphics.LEFT);

		g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD,
				Font.SIZE_MEDIUM));
		g.setColor(0x000000);
		
		//Text
		g.drawString(updateRange, 25, 80, Graphics.LEFT | Graphics.TOP);
		g.drawString(updateTimeInterval, 25, 125, Graphics.LEFT | Graphics.TOP);
		g.drawString(updateNrOfFields, 25, 170, Graphics.LEFT | Graphics.TOP);
		
		//RadioButtons
		int x = 150;
		int y = 80;
		for (int i = 0; i < posOfCursor.length; i++) {
			for (int j = 0; j < posOfCursor[i].length; j++) {
				if (posOfCursor[i][j] == 0) {
					g.drawImage(cursorEImg, x, y, Graphics.LEFT | Graphics.TOP);
				} else {
					g.drawImage(cursorFImg, x, y, Graphics.LEFT | Graphics.TOP);
				}
				y += 15;
			}
		}
		
		//Text dahinter
		g.drawString("2 fields", 170, 80, Graphics.LEFT | Graphics.TOP);
		g.drawString("5 fields", 170, 95, Graphics.LEFT | Graphics.TOP);
		g.drawString("10 fields", 170, 110, Graphics.LEFT | Graphics.TOP);
		
		g.drawString("1 min", 170, 125, Graphics.LEFT | Graphics.TOP);
		g.drawString("5 min", 170, 140, Graphics.LEFT | Graphics.TOP);
		g.drawString("10 min", 170, 155, Graphics.LEFT | Graphics.TOP);
		
		g.drawString("5 fields", 170, 170, Graphics.LEFT | Graphics.TOP);
		g.drawString("10 fields", 170, 185, Graphics.LEFT | Graphics.TOP);
		g.drawString("20 fields", 170, 200, Graphics.LEFT | Graphics.TOP);
		
		g.drawRect(168, (82 + (pointerPos * 15)), 70, 14);
	}

	/**
	 * Does the command action.
	 */
	public void commandAction(Command cmd, Displayable arg1) {
		if (cmd == okCmd) {
			CanvasManager.showOldScreen();
		} else if(cmd == chooseCmd) {
			keyPressed(getKeyCode(FIRE));
		}
	}

	/**
	 * Checks the position of the pointer and does the action when fire was pressed.
	 */
	protected void keyPressed(int keyCode) {
		switch (getGameAction(keyCode)) {
		case UP:
			pointerPos = --pointerPos < 0 ? nrOfItems - 1 : pointerPos;
			break;
		case DOWN:
			pointerPos = ++pointerPos >= nrOfItems ? 0 : pointerPos;
			break;
		case FIRE:
			setRadioButtons();
			break;
		default:
			switch (keyCode) {
			case KEY_NUM2:
				pointerPos = --pointerPos < 0 ? nrOfItems - 1 : pointerPos;
				break;
			case KEY_NUM8:
				pointerPos = ++pointerPos >= nrOfItems ? 0 : pointerPos;
				break;
			case KEY_NUM5:
				setRadioButtons();
				break;
			}
		}
		repaint();
	}
	
	/**
	 * Initializes the radio buttons.
	 *
	 */
	private void initRadioButtons() {
		posOfCursor = new byte[3][3];
		int i = MainController.getInstance().getStepUpdateIntervall();
		if(i == 2) {
			posOfCursor[0][0] = 1;
			posOfCursor[0][1] = 0;
			posOfCursor[0][2] = 0;
		} else if (i == 5) {
			posOfCursor[0][0] = 0;
			posOfCursor[0][1] = 1;
			posOfCursor[0][2] = 0;
		} else if (i == 10) {
			posOfCursor[0][0] = 0;
			posOfCursor[0][1] = 0;
			posOfCursor[0][2] = 1;
		} else {
			posOfCursor[0][0] = 0;
			posOfCursor[0][1] = 0;
			posOfCursor[0][2] = 0;
		}
		//RadioButtons for UpdateTimeInterval
		i = MainController.getInstance().getTimeIntervall();
		if (i == 60) {
			posOfCursor[1][0] = 1;
			posOfCursor[1][1] = 0;
			posOfCursor[1][2] = 0;
		} else if (i == 300) {
			posOfCursor[1][0] = 0;
			posOfCursor[1][1] = 1;
			posOfCursor[1][2] = 0;
		} else if (i == 600) {
			posOfCursor[1][0] = 0;
			posOfCursor[1][1] = 0;
			posOfCursor[1][2] = 1;
		} else {
			posOfCursor[1][0] = 0;
			posOfCursor[1][1] = 0;
			posOfCursor[1][2] = 0;
		}
		//RadioButtons for UpdateFieldInterval
		i = MainController.getInstance().getUpdateRange();
		if (i == 5) {
			posOfCursor[2][0] = 1;
			posOfCursor[2][1] = 0;
			posOfCursor[2][2] = 0;
		} else if (i == 10) {
			posOfCursor[2][0] = 0;
			posOfCursor[2][1] = 1;
			posOfCursor[2][2] = 0;
		} else if (i == 20) {
			posOfCursor[2][0] = 0;
			posOfCursor[2][1] = 0;
			posOfCursor[2][2] = 1;
		} else {
			posOfCursor[2][0] = 0;
			posOfCursor[2][1] = 0;
			posOfCursor[2][2] = 0;
		}
	}
	
	/**
	 * Sets the radio buttons.
	 *
	 */
	private void setRadioButtons() {
		//RadioButtons for UpdateRange
		if (pointerPos == 0) {
			posOfCursor[0][0] = 1;
			posOfCursor[0][1] = 0;
			posOfCursor[0][2] = 0;
			MainController.getInstance().setStepUpdateIntervall(2);
		} else if (pointerPos == 1) {
			posOfCursor[0][0] = 0;
			posOfCursor[0][1] = 1;
			posOfCursor[0][2] = 0;
			MainController.getInstance().setStepUpdateIntervall(5);
		} else if (pointerPos == 2) {
			posOfCursor[0][0] = 0;
			posOfCursor[0][1] = 0;
			posOfCursor[0][2] = 1;
			MainController.getInstance().setStepUpdateIntervall(10);
			
		//RadioButtons for UpdateTimeInterval
		} else if (pointerPos == 3) {
			posOfCursor[1][0] = 1;
			posOfCursor[1][1] = 0;
			posOfCursor[1][2] = 0;
			MainController.getInstance().setTimeIntervall(60);
		} else if (pointerPos == 4) {
			posOfCursor[1][0] = 0;
			posOfCursor[1][1] = 1;
			posOfCursor[1][2] = 0;
			MainController.getInstance().setTimeIntervall(300);
		} else if (pointerPos == 5) {
			posOfCursor[1][0] = 0;
			posOfCursor[1][1] = 0;
			posOfCursor[1][2] = 1;
			MainController.getInstance().setTimeIntervall(600);
			
		//RadioButtons for UpdateFieldInterval
		} else if (pointerPos == 6) {
			posOfCursor[2][0] = 1;
			posOfCursor[2][1] = 0;
			posOfCursor[2][2] = 0;
			MainController.getInstance().setUpdateRange(5);
		} else if (pointerPos == 7) {
			posOfCursor[2][0] = 0;
			posOfCursor[2][1] = 1;
			posOfCursor[2][2] = 0;
			MainController.getInstance().setUpdateRange(10);
		} else if (pointerPos == 8) {
			posOfCursor[2][0] = 0;
			posOfCursor[2][1] = 0;
			posOfCursor[2][2] = 1;
			MainController.getInstance().setUpdateRange(20);
		}
	}
}
